import global from '@/utils/global'
import http from '@/utils/request'

export function getResourceMenuList() {
  return http({
    url: http.adornUrl(global.upms + '/resource/menu'),
    method: 'post'
  })
}

export function getList(name, status) {
  return http({
    url: http.adornUrl(global.upms + '/resource/list'),
    method: 'post',
    data: http.adornData({
      'name': name === '' ? null : name,
      'status': status === '' ? null : status
    })
  })
}

export function getResourceByRoleId(roleId) {
  return http({
    url: http.adornUrl(global.upms + '/resource/role/' + roleId),
    method: 'get'
  })
}

export function add(modelForm) {
  return http({
    url: http.adornUrl(global.upms + '/resource'),
    method: 'post',
    data: http.adornData({
      'name': modelForm.name,
      'vue': modelForm.vue,
      'path': modelForm.path,
      'url': modelForm.url,
      'method': modelForm.method,
      'resourceType': modelForm.resourceType,
      'permission': modelForm.permission,
      'icon': modelForm.icon,
      'hidden': modelForm.hidden,
      'alwaysShow': modelForm.alwaysShow,
      'noCache': modelForm.noCache,
      'sort': modelForm.sort === null ? 99 : modelForm.sort,
      'status': modelForm.status,
      'parentId': modelForm.parentIds
    })
  })
}

export function update(modelForm) {
  return http({
    url: http.adornUrl(global.upms + '/resource/' + modelForm.id),
    method: 'patch',
    data: http.adornData({
      'id': modelForm.id,
      'name': modelForm.name,
      'vue': modelForm.vue,
      'path': modelForm.path,
      'url': modelForm.url,
      'method': modelForm.method,
      'resourceType': modelForm.resourceType,
      'permission': modelForm.permission,
      'icon': modelForm.icon,
      'hidden': modelForm.hidden,
      'alwaysShow': modelForm.alwaysShow,
      'noCache': modelForm.noCache,
      'sort': modelForm.sort === null ? 99 : modelForm.sort,
      'status': modelForm.status,
      'parentId': modelForm.parentIds
    })
  })
}

export function deleteOne(id) {
  return http({
    url: http.adornUrl(global.upms + '/resource/batch/' + id),
    method: 'delete'
  })
}

export function deleteBatch(ids) {
  return http({
    url: http.adornUrl(global.upms + '/resource/delete/batches'),
    headers: { // application/json; charset=utf-8 //application/x-www-form-urlencoded; charset=utf-8
      'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    method: 'post',
    data: http.adornData({
      'ids': ids
    }, true, 'form')
  })
}

export function saveResourceByRoleId(roleId, resourceIds) {
  return http({
    url: http.adornUrl(global.upms + '/resource/roleResource'),
    headers: { // application/json; charset=utf-8 //application/x-www-form-urlencoded; charset=utf-8
      'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    method: 'post',
    data: http.adornData({
      'roleId': roleId,
      'resourceIds': resourceIds
    }, true, 'form')
  })
}
